package com.eimf.saf.common.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.eimf.saf.base.dao.BaseDao;
import com.eimf.saf.base.util.BaseUtil;
import com.eimf.saf.common.commons.annotation.NoI18nAnnotation;
import com.eimf.saf.common.entity.Internation;
import com.eimf.saf.common.util.CommonCache;

/**
 * @author eden
 * @date 2013-11-13 下午6:14:41
 * @Description: 国际化资源持久层
 */
@Repository
public class InternationDao extends BaseDao<Internation>
{

    @SuppressWarnings("unchecked")
    @NoI18nAnnotation
    public List<Map<Object, Object>> findAllInternation()
    {
        return this.getSqlMapClientTemplate().queryForList("InternationMap.findAllInternation");
    }

    public Long getSqe()
    {
        return (Long) super.getSqlMapClientTemplate().queryForObject("InternationMap.getSqe");
    }

    /**
     * 国际化新增
     * **/
    public Long addInternation(final Map<String, Object> map)
    {
        final Long seq = getSqe();
        map.put("ID", seq);
        super.getSqlMapClientTemplate().insert("InternationMap.insertInternation", map);
        updateAppCache(seq, map);
        return seq;
    }

    public int updInternation(final Map<String, Object> map)
    {
        return super.getSqlMapClientTemplate().update("InternationMap.updateInternation", map);
    }

    public int findCountInternation(final Map<String, Object> param)
    {
        return (Integer) super.getSqlMapClientTemplate().queryForObject("InternationMap.countInternation", param);
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> getInternationById(final long InternationId)
    {
        return super.getSqlMapClientTemplate().queryForList("InternationMap.getInternationById", InternationId);
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> getInternationByName(final String name)
    {
        return (Map<String, Object>) super.getSqlMapClientTemplate().queryForObject(
                "InternationMap.getInternationByName", name);
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> findInternation(final Map<String, Object> param)
    {
        List<Map<String, Object>> list = null;
        list = super.getSqlMapClientTemplate().queryForList("InternationMap.findInternationPage", param);
        for (final Map<String, Object> map : list)
        {
            map.put("CREATEDATE", BaseUtil.getDateTime(map.get("CREATEDATE").toString()));
        }
        return list;
    }

    // 更新缓存
    public Long updateAppCache(final Long seq, final Map<String, Object> map)
    {
        final Map<String, String> cachemap = new HashMap<String, String>();
        for (final Object key : map.keySet())
        {
            final Object value = map.get(key);
            cachemap.put(key + "", value != null ? value + "" : null);
        }
        CommonCache.setInternationMap(seq.toString(), cachemap);
        return seq;
    }

}
